package com.hsy.order.order.ui

import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.BaseAdapter
import com.flux.ui.adapter.FFragmentRecycleAdapter
import com.flux.utils.GsonUtil
import com.hsy.order.R
import com.hsy.order.base.ui.adapter.BaseHolder
import com.hsy.order.order.data.DeliveryData
import com.hsy.order.order.data.FoodData
import com.hsy.order.order.data.OrderData
import com.hsy.order.utils.CommonUtils
import kotlinx.android.synthetic.main.item_food.view.*
import kotlinx.android.synthetic.main.item_order.view.*
import java.lang.ref.WeakReference

/**
 * 首页适配器
 * Created on 2018/3/26
 *
 * @author Yang
 */
class OrderAdapter(fragment: ListFragment, var items: List<OrderData>, val onClick: ((Int, Int) -> Unit)) : FFragmentRecycleAdapter(WeakReference(fragment)) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = OrderHolder(getItemView(R.layout.item_order, parent))

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        val holder = viewHolder as OrderHolder
        val order = items[position]
        holder.itemView.orderCustomer.text = order.phone
        holder.itemView.orderStatus.text = order.status_name
        holder.itemView.orderAddress.text = getRes()?.getString(R.string.txt_order_address, order.address)
        holder.itemView.orderRemark.text = order.remark
        holder.itemView.orderNum.text = getRes()?.getString(R.string.txt_order_num, order.goods.size)
        holder.itemView.orderMoney.text = getRes()?.getString(R.string.txt_order_money, order.amount)
        holder.itemView.orderId.text = getRes()?.getString(R.string.txt_order_id, order.order_sn)
        holder.itemView.orderTime.text = getRes()?.getString(R.string.txt_order_time, order.create_datetime_str)

        val adapter = FoodsAdapter(order.goods, order.boxfee, order.boxqty, order.coupon, order.freight, LayoutInflater.from(mFragment.get()?.context))
        holder.itemView.orderFoods.adapter = adapter
        adapter.notifyDataSetChanged()

        when (order.type) {
            "1" -> {
                holder.itemView.orderTypeIn.visibility = View.VISIBLE
                holder.itemView.orderTypeSelf.visibility = View.GONE
                holder.itemView.orderTypeOut.visibility = View.GONE
            }
            "2" -> {
                holder.itemView.orderTypeIn.visibility = View.GONE
                holder.itemView.orderTypeSelf.visibility = View.VISIBLE
                holder.itemView.orderTypeOut.visibility = View.GONE
            }
            "3" -> {
                holder.itemView.orderTypeIn.visibility = View.GONE
                holder.itemView.orderTypeSelf.visibility = View.GONE
                holder.itemView.orderTypeOut.visibility = View.VISIBLE
            }
        }
        //订单按钮显示
        when (order.status) {
            "5" -> {
                holder.itemView.orderRefuse.visibility = View.VISIBLE
                holder.itemView.orderPrint.visibility = View.VISIBLE
                holder.itemView.orderAccept.visibility = View.VISIBLE
                holder.itemView.orderDispatch.visibility = View.GONE
            }
            "10" -> {
                holder.itemView.orderRefuse.visibility = View.GONE
                holder.itemView.orderPrint.visibility = View.VISIBLE
                holder.itemView.orderAccept.visibility = View.GONE
                if (order.type == "3" && !CommonUtils.getAutoDispatch()) holder.itemView.orderDispatch.visibility = View.VISIBLE
            }
            "100" -> {
                holder.itemView.orderRefuse.visibility = View.GONE
                holder.itemView.orderPrint.visibility = View.GONE
                holder.itemView.orderAccept.visibility = View.GONE
                holder.itemView.orderDispatch.visibility = View.GONE
            }
            else -> {
                holder.itemView.orderRefuse.visibility = View.GONE
                holder.itemView.orderPrint.visibility = View.VISIBLE
                holder.itemView.orderAccept.visibility = View.GONE
                holder.itemView.orderDispatch.visibility = View.GONE
            }
        }

        //是否显示骑手
        if (order.logistics_status != "2") holder.itemView.orderDelivery.visibility = View.GONE
        else {
            holder.itemView.orderDelivery.visibility = View.VISIBLE
            val delivery = GsonUtil.fromJson(order.logistics_msg, DeliveryData::class.java)
            holder.itemView.orderDelivery.text = getRes()?.getString(R.string.txt_order_delivery, delivery.dm_name, delivery.dm_mobile)
        }
        //是否显示地址
        holder.itemView.orderAddress.visibility = if (order.address == "") View.GONE else View.VISIBLE
        //是否展开显示
        if (holder.showMenu) {
            holder.itemView.orderFoods.visibility = View.VISIBLE
            holder.itemView.button.text = "收起"
        } else {
            holder.itemView.orderFoods.visibility = View.GONE
            holder.itemView.button.text = "展开"
        }
        //是否显示备注
        holder.itemView.orderRemarkLayout.visibility = if (order.remark == "") View.GONE else View.VISIBLE

        holder.itemView.button.setOnClickListener {
            holder.showMenu = !holder.showMenu
            if (holder.showMenu) {
                holder.itemView.orderFoods.visibility = View.VISIBLE
                holder.itemView.button.text = "收起"
            } else {
                holder.itemView.orderFoods.visibility = View.GONE
                holder.itemView.button.text = "展开"
            }
        }
        holder.itemView.orderRefuse.setOnClickListener { onClick(0, position) }
        holder.itemView.orderPrint.setOnClickListener { onClick(1, position) }
        holder.itemView.orderAccept.setOnClickListener { onClick(2, position) }
        holder.itemView.orderDispatch.setOnClickListener { onClick(3, position) }
    }

    override fun getItemCount() = items.size

    inner class OrderHolder(view: View) : BaseHolder(view) {
        var showMenu = false
    }

    private inner class FoodsAdapter(val foods: List<FoodData>, val boxfee: String, val boxqty: String, val coupon: String, val freight: String, val inflater: LayoutInflater) : BaseAdapter() {
        override fun getCount() = foods.size + 3

        override fun getItem(position: Int) = when (position) {
            foods.size -> FoodData("", "餐盒费", boxfee, boxqty, "")
            foods.size + 1 -> FoodData("", "红包优惠", "-$coupon", "", "")
            foods.size + 2 -> FoodData("", "配送费", freight, "", "")
            else -> foods[position]
        }

        override fun getItemId(position: Int) = position.toLong()

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var view = convertView
            if (view == null) {
                view = inflater.inflate(R.layout.item_food, parent, false)!!
            }
            val hasQty = getItem(position).qty != "" && getItem(position).qty != "0"
            val money = if (!hasQty) getItem(position).price
            else (getItem(position).qty.toInt() * getItem(position).price.toFloat()).toString()

            view.itemName.text = getItem(position).name
            view.itemNum.text = if (hasQty) "x${getItem(position).qty}" else ""
            view.itemPrice.text = getRes()?.getString(R.string.txt_order_money, money)
            return view
        }
    }
}